﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity;
using System.Linq;
using System.Data;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.PersonalInformationDocument.SocialRelationPropertyRecord
{
    public partial class SocialRelationPropertyRecordUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        int ID = 1;
        string listName = JCYConstant.SharePointListName.PropertyRecord;
        string currentAccount = SPContext.Current.Web.CurrentUser.Name;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CodeTable = GetPPSM2007CODEData();
              
                var currentUserName = SPContext.Current.Web.CurrentUser.Name;
               
                DataRow[] relationRows = GetRelationInfoByName(currentUserName).Select();
                dlSocialRelation.DataSource = relationRows;
                dlSocialRelation.DataBind();
                if (relationRows.Count() > 0)
                {
                    SocialRelationNoRecord.Visible = false;
                }
                    
                else
                    SocialRelationNoRecord.Visible = true;
               
               



                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists[listName];
                lvProperty.ListId = list.ID.ToString();
                //defualt user ListView == MyView (Display my records)
                lvProperty.ViewId = list.Views["MyView"].ID.ToString();

                string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", list.DefaultNewFormUrl);
                this.idHomePageNewItem.Attributes.Add("onclick", onclick);
          
               
            }
        }
        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }
        public void BindPropertyInformation()
        { 
        
        }
        protected void dlSocialRelation_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserRelationDataSet.UserRelationRow row = (PPSMUserRelationDataSet.UserRelationRow)e.Item.DataItem;
            Label lbID = (Label)e.Item.FindControl("lbID");
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbRelationName = (Label)e.Item.FindControl("lbRelationName");
            Label lbBirth = (Label)e.Item.FindControl("lbBirth");
            Label lbWorkPlace = (Label)e.Item.FindControl("lbWorkPlace");
            Label lbJobTitle = (Label)e.Item.FindControl("lbJobTitle");
            Label lbPoliticalLandscape = (Label)e.Item.FindControl("lbPoliticalLandscape");
            lbID.Text = Convert.ToString(e.Item.ItemIndex + 1);
            ID++;
            lbName.Text = string.IsNullOrEmpty(row.RelationName) ? string.Empty : row.RelationName;
            lbRelationName.Text = string.IsNullOrEmpty(row.UserRelation) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "关系称谓", row.UserRelation);
            lbBirth.Text = string.IsNullOrEmpty(row.Birthday) ? string.Empty : row.Birthday.Substring(0, 4).ToString() + "年";
            lbWorkPlace.Text = string.IsNullOrEmpty(row.WorkDept) ? string.Empty : row.WorkDept;
            lbJobTitle.Text = string.IsNullOrEmpty(row.AdminRank) ? string.Empty : row.AdminRank;
            lbPoliticalLandscape.Text = string.IsNullOrEmpty(row.Clan) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "政治面貌", row.Clan);
        }
        public PPSMUserRelationDataSet.UserRelationDataTable GetRelationInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetRelationInfoByName(userName);
            }
        }

        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt, string category, string value)
        {
            try
            {
                var codeDesc = from codeTable in dt
                               where codeTable.Category == category && codeTable.CodeValue == value
                               select codeTable.CodeDesc;
                return codeDesc.First();
            }
            catch (Exception)
            {

                return "";
            }
          


        }

       
        public void BindUserRelationInfo(PPSMUserRelationDataSet.UserRelationDataTable userRelationInfoDT)
        { 
        
        }


    }
}
